// component/dropdown-item-picker/dropdown-item-picker.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        height: {
            type: String,
            value: '488rpx'
        },
        dataList: { //对象，传入待筛选项信息
            type: Object,
        },
        vipLimit: {//字符串，表明是哪种vip限制功能
            type: String
        }
        /* dataList示例
          dataList: {
              '不限': [],
              '幼儿园': ['不限', '语文老师', '数学老师'],
              '小学': ['不限', '语文老师', '数学老师', '英语老师', '思想品德老师', '科学技术老师'],
              '初中': ['不限', '语文老师', '数学老师', '英语老师', '思想品德老师', '科学技术老师'],
              '高中': ['不限', '语文老师', '数学老师', '英语老师', '思想品德老师', '科学技术老师'],
              '高职': ['不限', '语文老师', '数学老师', '英语老师', '思想品德老师', '科学技术老师'],
          },
        */
    },
    // lifetimes: {
    //     attached: function() {
    //         const firstList = [];
    //         console.log("这是attached时", this.properties.dataList)
    //         for (let item in this.properties.dataList) {
    //             firstList.push(item);
    //         }

    //         const orignFirstValue = firstList[0];
    //         const secondList = this.properties.dataList[orignFirstValue];
    //         const orignSecondValue = secondList[0];

    //         // 在组件实例进入页面节点树时执行
    //         this.setData({
    //             firstList: firstList,
    //             secondList: secondList,

    //             orignFirstIdx: 0,
    //             orignFirstValue: orignFirstValue,
    //             orignSecondIdx: 0,
    //             orignSecondValue: orignSecondValue,

    //             currentFirstvalue: orignFirstValue,
    //             currentSecondvalue: orignSecondValue,


    //         })
    //         console.log("这是", this.properties.dataList)

    //     },
    // },
    /**
     * 之所以用observers不用attached是因为
     * observers在每次props.dataList更新时都会触发，而attached只在组件实例化时(早于父页面实例化)被触发
     * 而此该组件的wxml没有用到props.dataList,而是用到由props.dataList计算而来的firstList,secondList等属性
     * 故而若使用attached，在传入的dataList更新时，不会触发组件的自动更新(因为该组件的wxml没有用到props.dataList)
     * 若使用observers,在传入的dataList更新时，都会重新计算firstList,secondList等属性,故而会触发视图更新
     */
    observers: {
        'dataList': function(dataList) {
            // console.log('dataList更新啦', dataList)
            const firstList = [];
            // console.log("这是observers", this.properties.dataList)
            for (let item in this.properties.dataList) {
                firstList.push(item);
            }

            const orignFirstValue = firstList[0];
            var secondList = this.properties.dataList[orignFirstValue];
            if(secondList==undefined)
            {
                secondList=[]
            }
            const orignSecondValue = secondList[0];

            // 在组件实例进入页面节点树时执行
            this.setData({
                firstList: firstList,
                secondList: secondList,

                orignFirstIdx: 0,
                orignFirstValue: orignFirstValue,
                orignSecondIdx: 0,
                orignSecondValue: orignSecondValue,

                currentFirstvalue: orignFirstValue,
                currentSecondvalue: orignSecondValue,


            })
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        //原始数据
        orignFirstIdx: 0,
        // orignFirstValue
        orignSecondIdx: 0,
        // orignSecondValue
        //动态数据
        currentFirstIdx: 0,
        // currentFirstvalue
        currentSecondIdx: 0,
        currentSecondvalue: "",
        // firstList
        secondList: null
    },

    /**
     * 组件的方法列表
     */
    methods: {
        tapFirstColumn(e) {
            const currentFirstIdx = e.currentTarget.dataset.index;
            const currentFirstvalue = e.currentTarget.dataset.value;
            const secondList = this.properties.dataList[currentFirstvalue];
            this.setData({
                currentFirstIdx: currentFirstIdx,
                currentFirstvalue: currentFirstvalue,
                secondList: secondList,
                currentSecondIdx: 0,
                currentSecondvalue: secondList[0]

            })
        },
        tapSecondColumn(e) {
            const currentSecondIdx = e.currentTarget.dataset.index;
            const currentSecondvalue = e.currentTarget.dataset.value;
            this.setData({
                currentSecondIdx: currentSecondIdx,
                currentSecondvalue: currentSecondvalue
            })
        },
        tapReset() { //点击重置按钮
            const currentFirstIdx = this.data.orignFirstIdx;
            const currentFirstvalue = this.data.orignFirstValue;
            const currentSecondIdx = this.data.orignSecondIdx;
            const currentSecondvalue = this.data.orignSecondValue;
            const secondList = this.properties.dataList[currentFirstvalue];
            this.setData({
                currentFirstIdx: currentFirstIdx,
                currentFirstvalue: currentFirstvalue,
                currentSecondIdx: currentSecondIdx,
                currentSecondvalue: currentSecondvalue,
                secondList: secondList
            });
        },
        async tapConfirm() { //点击确定按钮
            var flag=0
            if(this.data.vipLimit=="jobLimit"){
                //先判断是否登录
            if(getApp().globalData.isLogin==undefined){
                await wx.cloud.callFunction({
                     name: "inviteUser",
                     data: {
                       sign: 5
                     },
                     success: res=>{
                       //如果该用户未登陆过
                   if(res.result.length==0){
                     wx.showToast({
                       title: '请先登录',
                       icon: "error",
                       duration: 1000
                     })
                     setTimeout(function(){
                         wx.navigateTo({
                             url: "/pages/login_main/login_main",
                             success: (result) => {
                   
                             },
                             fail: () => {},
                             complete: () => {}
                         });
                     },1000)          
                   }else{
                       getApp().globalData.isLogin=true
                       flag=1
                   }
                     }
                   })
                 }
                 else{
                     flag=1
                 }
                 if(flag==0){
                     console.log("sssssss")
                     return ;
                 }
                //判断岗位限制
                // let temp=await wx.cloud.callFunction({
                //     name: "vipLimit",
                //     data: {
                //         sign: 0,
                //     }
                // })
                // temp=temp.result
                // if(temp.errCode==1){
                //     wx.showToast({
                //       title: '查看次数已用完!',
                //       icon: 'error',
                //       duration: 2000,
                //   success:res =>{
                //     setTimeout(function(){
                //         wx.navigateTo({
                //             url: '../../pages/huiyuan/huiyuan',
                //           })
                //     },2000)
                //   }
                //     })
                //     return ;
                // }
            }
            this.triggerEvent('filter', { firstVal: this.data.currentFirstvalue, secondVal: this.data.currentSecondvalue || '' }); //触发父组件的filter事件
        },
    }
})